<template>
	<view>
		<button @click="onUpdateMethod" v-if="onUpdate">更新</button>
		<button @click="offUpdateMethod" v-if="offUpdate">更新中 {{progress}}</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				progress: 0,
				onUpdate: true,
				offUpdate: false
			}
		},
		onLoad: function(res) {
			uni.setNavigationBarTitle({
			　　title:'res.data.storeName'
			})
		},
		methods: {
			offUpdateMethod() {
				this.onUpdate = true;
				this.offUpdate = false;
			},
			onUpdateMethod() {
				this.onUpdate = false;
				this.offUpdate = true;
				let seft = this;
				this.CheckVersion().then((result) => {
					if (result.data.ActionOK) {
						if (!result.data.Data.ishot) {
							uni.runtime.openURL({
								url: result.data.Data.wgeturl
							})
						} 
						else{
							const downloadTask = uni.downloadFile({
								url: result.data.Data.wgeturl,
								success: (downloadResult) => {
									if (downloadResult.statusCode === 200) {
										console.error('idownloadResult.', downloadResult);
										plus.runtime.install(downloadResult.tempFilePath, {
											force: false
										}, function() {
											plus.runtime.restart();
											console.log('install success...');
										}, function(e) {
											uni.showModal({
												title: 'error',
												content: '更新失败，请稍后重试！',
												showCancel: false,
												confirmText: 'ok'
											})

											console.error('install error fail');
										});
									}
								}
							});
							downloadTask.onProgressUpdate((res) => {
								console.log('下载进度' + res.progress);
								seft.progress = res.progress

								// 测试条件，取消下载任务。
								//if (res.progress > 50) {
								//downloadTask.abort();
								//}
							});
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
